// Created by Amit Mudgal (Principal Consultant - MicroStrategy Professional Services ) on 6/16/2014 for GMS

package com.amex.SQL;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;





public class QueryExecution {

	
	
	public static Connection SQLconnect(String SQLServer,String SQLServerDB,String SQLServerUID, String SQLServerPWD,String drivername,String JDBC_URL,String port) 
	{
		String METHOD_NAME = "SQLconnect";
		Connection conn = null;
		
		String connectionURL = JDBC_URL + SQLServer + ":" + port + "/" + SQLServerDB ;
		//String connectionURL ="jdbc:jtds:sqlserver://" + SQLServer + ":1433/" + SQLServerDB ;
		//String connectionURL ="jdbc:netezza://" + SQLServer +":5480/" + SQLServerDB;
		try {
			Class.forName(drivername);
			conn = DriverManager.getConnection(connectionURL,SQLServerUID,SQLServerPWD);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}

	public static boolean SQLDisconnect(Connection conn) {
		String METHOD_NAME = "SQLDisconnect";
		try {
			conn.close();
		} catch (SQLException e)  { 
			return false;
		}
	return true;
	}
	
	public static ResultSet ExecSQLQuery(Connection conn, String SQL) {
		String METHOD_NAME =  "ExecSQLQuery";
		ResultSet rs = null;
		//System.out.println(SQL);
		try {
		  Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
		  rs = stmt.executeQuery(SQL);
		} catch (Exception e) {System.out.println(e);
			}
		return rs;
	}
	
    public static void UpdateSQL(Connection conn,String UpdateSQL){
    	
    	int rsd;    	
    	try {
			Statement stmt = conn.createStatement();
			rsd = stmt.executeUpdate(UpdateSQL);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
		return;    	
    	
    }
	
}
